﻿using System;
using System.Collections.Generic;
using System.Windows;
using NetBay.Core.BusinessEntities;
using Rio.Framework.Pages.Context;
using Rio.Framework.Pages.Controls;
using System.Linq;
using Telerik.Windows.Controls;

namespace Rio.Search.Application.Controls
{
    public partial class SearchHomeTab
    {
        public delegate void SearchResultEventHandler(Folder folder, Document document);
        public event SearchResultEventHandler SearchResultClicked;

        public delegate void DocumentPastedEventHandler(long folderId);
        public event DocumentPastedEventHandler DocumentPasted;

        public List<FolderPage> FolderPages
        {
            get { return (List<FolderPage>)GetValue(FoldersProperty); }
            set { SetValue(FoldersProperty, value); }
        }
        public static readonly DependencyProperty FoldersProperty =
            DependencyProperty.Register("FolderPages", typeof(List<FolderPage>), typeof(SearchHomeTab), null);

        public Organization CurrentOrganization
        {
            get { return searchPanel.m_oCurrentOrganization; }
        }

        public FolderType CurrentFolderType
        {
            get { return searchPanel.m_oCurrentFolderType; }
        }

        public Organization Organization
        {
            get { return searchPanel.m_oOrganization; }
        }

        public FolderType FolderType
        {
            get { return searchPanel.m_oFolderType; }
        }

        protected void OnSearchResultClicked(Folder f)
        {
            if (SearchResultClicked != null)
                SearchResultClicked(f, null);
        }

        protected void OnSearchResultClicked(Document d)
        {
            if (SearchResultClicked != null)
                SearchResultClicked(null, d);
        }

        protected void OnDocumentPasted(long folderId)
        {
            if (DocumentPasted != null)
                DocumentPasted(folderId);
        }

        public SearchHomeTab()
        {
            InitializeComponent();

            InitLabel();
            
            searchPanel.ListIndexPropertyChanged += SearchPanelListIndexPropertyChanged;
            searchPanel.FolderSearchPropertyChanged += SearchPanelFolderSearchPropertyChanged;
            searchPanel.DocumentSearchPropertyChanged += SearchPanelDocumentSearchPropertyChanged;
            searchPanel.ClearCriteriasProcessed += SearchPanelClearCriteriasProcessed;

            documentsResult.LoadModificationSelectedDocument += OnSearchResultClicked;

            foldersResult.LoadModificationSelectedDocument += OnSearchResultClicked;
            foldersResult.LoadModificationSelectedFolder += OnSearchResultClicked;
            foldersResult.CreateAlertEventHandler += FolderResultControlRefreshSearchResult;
            foldersResult.RefreshSearchResult += FolderResultControlRefreshSearchResult;
            foldersResult.DocumentPasted += FolderResultControlDocumentPasted;

            foldersResult.Visibility = Visibility.Visible;
            documentsResult.Visibility = Visibility.Collapsed;
        }

        public override void InitLabel()
        {
            base.InitLabel();

            searchPanel.InitLabel();
        }

        private void SearchPanelClearCriteriasProcessed(object sender, System.EventArgs e)
        {
            if (foldersResult != null)
                foldersResult.ButtonAlert.IsEnabled = false;
        }

        private void SearchPanelListIndexPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            foldersResult.ClearResults();
            foldersResult.ListIndex = (List<Index>)e.NewValue;

            documentsResult.ClearResults();
            documentsResult.ListIndex = (List<Index>)e.NewValue;

            ((RadMenuItem)foldersResult.FindName("AddDocumentContextMenu")).IsEnabled = GlobalWebContext.GetUserCanImport(searchPanel.m_oOrganization, searchPanel.m_oFolderType);
        }

        private void FolderResultControlDocumentPasted(object sender, Folder folder)
        {
            if (folder != null)
                OnDocumentPasted(folder.Identifier);
        }

        private void FolderResultControlRefreshSearchResult(object sender, EventArgs e)
        {
            searchPanel.Search();
        }

        private void SearchPanelFolderSearchPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            foldersResult.ClearResults();

            foldersResult.FolderTypeSelected = searchPanel.m_oFolderType;
            foldersResult.OrganizationSelected = searchPanel.m_oOrganization;
            foldersResult.ActualSearchPanelWidth = searchPanel.ActualWidth;
            foldersResult.InitGridViewFolderSearch();
            foldersResult.ListFolders = (List<Folder>)e.NewValue;

            documentsResult.Visibility = Visibility.Collapsed;
            foldersResult.Visibility = Visibility.Visible;
        }

        private void SearchPanelDocumentSearchPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            documentsResult.ClearResults();

            documentsResult.FolderTypeSelected = searchPanel.m_oFolderType;
            documentsResult.InitGridViewFolderSearch();
            documentsResult.ListDocuments = (List<Document>)e.NewValue;

            foldersResult.Visibility = Visibility.Collapsed;
            documentsResult.Visibility = Visibility.Visible;
        }

        public override void Dispose()
        {
            searchPanel.ListIndexPropertyChanged -= SearchPanelListIndexPropertyChanged;
            searchPanel.FolderSearchPropertyChanged -= SearchPanelFolderSearchPropertyChanged;
            searchPanel.DocumentSearchPropertyChanged -= SearchPanelDocumentSearchPropertyChanged;
            searchPanel.ClearCriteriasProcessed -= SearchPanelClearCriteriasProcessed;

            documentsResult.LoadModificationSelectedDocument -= OnSearchResultClicked;

            foldersResult.LoadModificationSelectedDocument -= OnSearchResultClicked;
            foldersResult.LoadModificationSelectedFolder -= OnSearchResultClicked;
            foldersResult.CreateAlertEventHandler -= FolderResultControlRefreshSearchResult;
            foldersResult.RefreshSearchResult -= FolderResultControlRefreshSearchResult;
            foldersResult.DocumentPasted -= FolderResultControlDocumentPasted;

            base.Dispose();
        }
    }
}
